package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.StemealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    StemealDishMapper setmealMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    StemealDishMapper setmealDishMapper;


    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page page = (Page) setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
        List<SetmealVO> setmealVOS = new ArrayList<>();
        for (Setmeal setmeal : result) {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            String s = categoryMapper.selectNameId(setmeal.getCategoryId());
            setmealVO.setCategoryName(s);
            setmealVOS.add(setmealVO);
        }

        return  new PageResult(total, setmealVOS);
    }

    @Override
    public SetmealVO queryById(Long id) {
        Setmeal setmeal =  setmealMapper.queryById(id);
        List<SetmealDish>  setmealDishList = setmealMapper.selectByDishId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        setmealMapper.updateStatus(status,id);
    }

    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null && setmealDishes.size()>0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());

            }setmealDishMapper.insertBatch(setmealDishes);
        }
    }
    @Override
    public void deleteBatch(List<Long> ids) {
        Integer startCount = setmealMapper.countStart(ids);
        if (startCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        setmealMapper.removeBatch(ids);
        setmealDishMapper.removeBatch1(ids);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //更新套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除当前套餐所有的套餐菜品信息
        setmealDishMapper.deleteByDishIds(setmeal.getId());
        //3. 批量添加当前套餐新的菜品列表
        List<SetmealDish> flavors = setmealDTO.getSetmealDishes();

        //修改口味

        if(flavors != null && flavors.size() > 0){
            for (SetmealDish flavor : flavors) {
                flavor.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insertBatch(flavors);
        }
    }

    @Override
    public List<Setmeal> listByCategoryId(Long categoryId) {
        return setmealMapper.listByCategoryId(categoryId);
    }

}



